let requestUrl = null;

function addHandler() {
	let categoryId = $("#category_id").combobox("getValue");

	if (categoryId) {
		$("#categoryId").combobox("setValue", categoryId);
	}

	requestUrl = "/grade_exp/insert";

	$("#exp_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#exp_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/grade_exp/updateById";

		$("#id").val(rowData.id);
		$("#exp").numberspinner("setValue", rowData.exp);
		$("#grade").numberspinner("setValue", rowData.grade);
		$("#categoryId").combobox("setValue", rowData.categoryId);

		$("#exp_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

$(document).ready(function() {

	$("#category_id").combobox({
		url: "/exp_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		editable: true,
		panelHeight: "auto",
		prompt: "-选择经验类型-"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let categoryId = $("#category_id").combobox("getValue");

		$("#exp_list").datagrid("reload", {
			categoryId: categoryId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#categoryId").combobox({
		url: "/exp_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		editable: true,
		required: true,
		panelHeight: "auto"
	});

	$("#exp").numberspinner({
		min: 1,
		width: 150,
		required: true
	});

	$("#grade").numberspinner({
		min: 1,
		width: 150,
		required: true
	});

	$("#exp_dialog").dialog({
		title: "等级经验",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#exp_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#exp_dialog").dialog("close");
						$("#exp_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#exp_form").form("clear");
				$("#exp_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr * 15;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;
	/**
	 * 可选页码
	 * @type {number[]}
	 */
	let pageList = [15, 30, 100, 200, 500];

	$("#exp_list").datagrid({
		url: "/grade_exp/selectByPage",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		pageList: pageList,
		pageSize: pageList[0],
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "categoryId", title: "经验类型", align: "center", width: 40
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/exp_category/selectById", {
							id: value
						}, function(resp) {
							let data = resp.data;

							$("#category_" + rowIndex).html(data.name);
						}, error);

						return "<div id='category_" + rowIndex + "'></div>";
					}
				}
			},
			{field: "grade", title: "等级", align: "center", width: 100
				, formatter: function(value) {
					return "<div>" + value + "级</div>";
				}
			},
			{field: "exp", title: "经验值", align: "center", width: 100}
		]]
	});

});